<?php
/*
 * http://wange.im/highlight-keywords-and-hellobaby-in-wordpress.html
 */
function ls_get_delim($ref) {
	$search_engines = array    (
                            'google.com' => 'q',
                            'google.com.hk' => 'q',
                            'baidu.com' => 'wd',
                            'search.yahoo.com' => 'p',
                            'cn.search.yahoo.com' => 'p',
                            'cn.bing.com' => 'q',
                            'search.lycos.com' => 'query',
                            'youdao.com' => 'q',
                            'search.aol.com' => 'query',
                            'ask.com' => 'q',
                            'hotbot.com' => 'query',
                            'overture.com' => 'Keywords',
                            'search.netscape.com' => 'query',
                            'search.looksmart.com' => 'qt',
                            'search.earthlink.net' => 'q',
                            'search.viewpoint.com' => 'k',
                            'mamma.com' => 'query'
                            );
                            $delim = FALSE;
                            if (isset($search_engines[$ref])) {
                            $delim = $search_engines[$ref];
    }
	return $delim;
}
function ls_get_terms($d) {
	$terms = null;
	$query_array = array();
	$query_terms = null;
	$query = explode($d.'=', $_SERVER['HTTP_REFERER']);
	$query = explode('&', $query[1]);
	$query = urldecode($query[0]);
	$query = str_replace("'", '', $query);
	$query = str_replace('"', '', $query);
	$query_array = preg_split('/[\s,\+\.]+/',$query);
	$query_terms = implode(' ', $query_array);
	$terms = htmlspecialchars(urldecode($query_terms));
	if (!seems_utf8($terms)){
		$terms=iconv("GBK", "UTF-8", $terms);
	}
	return $terms;
}
function ls_get_refer() {
	if (!isset($_SERVER['HTTP_REFERER']) || ($_SERVER['HTTP_REFERER'] == '')) return FALSE;
	$referer_info = parse_url($_SERVER['HTTP_REFERER']);
	$referer = $referer_info['host'];
	if (substr($referer, 0, 4) == 'www.')
	$referer = substr($referer, 4);

	return $referer;
}
function ls_getinfo($what) {
	$referer = ls_get_refer();
	if (!$referer) return FALSE;
	$delimiter = ls_get_delim($referer);

	if ($delimiter)
	{
		$terms = ls_get_terms($delimiter);

		if ($what == 'isref') {
			return ($terms != ''?true:false);
		}
		if ($what == 'referrer') {
			$parsed = parse_url($_SERVER['HTTP_REFERER']);
			echo '<a href="http://'.$parsed['host'].'">'.$parsed['host'].'</a>';
		}
		if ($what == 'terms') { echo $terms; }
			
	}
}
function  ls_terms_filter($content){
	$referer = ls_get_refer();
	$output = '';
	$delimiter = ls_get_delim($referer);
	if($delimiter)
	{
		$terms = ls_get_terms($delimiter);
		$terms= array_unique(preg_split("/[\s,]+/", $terms));
		$contentarr = preg_split("/(<.*>)/U", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
		$stop = count($contentarr);
		for ($i = 0; $i < $stop; $i++) {
			$content = $contentarr[$i];
			if ((strlen($content) > 0) && ('<' != $content{0})) {
				foreach ($terms as $term) {
					if (strlen($term)>1)
					$content=eregi_replace("($term)","<mark>\\1</mark>", $content);
				}
			}
			$output .= $content;
		}
		return $output;
	} else{
		return $content;
	}
}
add_filter('the_content', 'ls_terms_filter');
add_filter('comment_text', 'ls_terms_filter');
function ls_set_header() {
	if (ls_getinfo('isref')) header('Vary: Referer', FALSE);
}
add_action('init', 'ls_set_header');
?>